package top.z.work.module.system.controller.authorize;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.z.work.framework.common.pojo.R;
import top.z.work.framework.web.annotation.AccessLogger;
import top.z.work.module.system.controller.authorize.vo.AuthorizeCaptchaVO;
import top.z.work.module.system.controller.authorize.vo.AuthorizeInfoVO;
import top.z.work.module.system.controller.authorize.vo.AuthorizeLoginVO;
import top.z.work.module.system.service.AuthorizeService;

/**
 * 认证管理
 */
@Tag(name = "认证管理")
@Slf4j
@Validated
@RestController
@RequestMapping("system/authorize")
public class AuthorizeController {

    @Resource
    private AuthorizeService authorizeService;

    /**
     * 获取验证码
     */
    @AccessLogger(module = "认证管理", title = "获取验证码")
    @Operation(summary = "获取验证码")
    @GetMapping("captcha")
    public R<AuthorizeCaptchaVO> getCaptcha() {
        return R.s(authorizeService.generate());
    }

    /**
     * 账号登录
     */
    @AccessLogger(module = "认证管理", title = "账号登录")
    @Operation(summary = "账号登录")
    @PostMapping("login")
    public R<String> login(@RequestBody @Validated AuthorizeLoginVO vo) {
        return R.s("登录成功", authorizeService.login(vo));
    }

    /**
     * 退出登录
     */
    @AccessLogger(module = "认证管理", title = "退出登录")
    @Operation(summary = "退出登录")
    @PostMapping("logout")
    public R<String> logout() {
        authorizeService.logout();
        return R.<String>s().setMessage("退出成功");
    }

    /**
     * 获取登录信息
     */
    @AccessLogger(module = "认证管理", title = "获取登录信息")
    @GetMapping("info")
    @Operation(summary = "获取登录信息")
    public R<AuthorizeInfoVO> info() {
        return authorizeService.accountInfo();
    }

}
